﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbConnect.Sql;
using DbConnect;

namespace DnDEngine.Class
{
    public class Request
    {
        #region "Private attributes"

        private int _ID;

        private List<Parameter> _parameters;

        private int _webSite;

        #endregion

        #region "Properties"

        public String Method { get; set; }

        public RespType ResponseType { get; set; }

        public ReqType RequestType { get; set; }

        public Boolean Param { get; set; }

        public List<Parameter> Parameters
        {
            get { return _parameters; }
        }

        public String Url { get; set; }

        public int Order { get; set; }

        public int Index
        {
            get { return this._ID; }
        }

        public int WebSite { get; set; }


        #endregion

        #region "Events"
        #endregion

        #region "Public functions"

        public Request()
        {
            try
            {
                Logger.Log.TraceAction("Request.Request", "");
            _ID = -1;
            _parameters = new List<Parameter>();
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.Request", ex);
            }
        }

        public Request(String reqUrl)
        {
            try
            {
                Logger.Log.TraceAction("Request.Request", reqUrl);
                this.Url = reqUrl;
                //Insert request
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.Request", ex);
            }


        }

        public Request(Int16 ID)
        {
            try
            {
                Logger.Log.TraceAction("Request.Request", ID.ToString());
                this._ID = ID;
                this.Load(this._ID);
                this.LoadParameter();
                //LoadLibelle();
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.Request", ex);
            }
        }

        public String GetRequestWithParams()
        {
            var reqPlusParam = Url;
            try
            {
                Logger.Log.TraceAction("Request.GetRequestWithParams", reqPlusParam);
                foreach (var Param in Parameters)
                {
                    reqPlusParam = reqPlusParam.Replace(Param.Name, Param.Value);
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.GetRequestWithParams", ex);
            }
            return reqPlusParam;
        }

        public void AddParam(String Name, String Value)
        {
            try
            {
                Logger.Log.TraceAction("Request.AddParam", Name + Value);
                Parameters.Add(new Parameter(Name, Value));
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.AddParam", ex);
            }


        }

        public void DeleteParam(String Name)
        {
            try
            {
                Logger.Log.TraceAction("Request.DeleteParam", Name);
                foreach (var Param in Parameters)
                {
                    if (Param.Name == Name)
                    {
                        Parameters.Remove(Param);
                        Param.Delete();
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.DeleteParam", ex);
            }

        }

        #region "Bdd functions

        //Will be load when website load

        public void Load(int index)
        {

            try
            {
                Logger.Log.TraceAction("Request.LoadRequest", index.ToString());
                var req = SqlWRequest.Select;
               
                var data = new DbConnect.DataAccess();
                 data.AddParam(ParamWRequest.ID, index);
                var table = data.SelectWithParam(req);

                if (table.Rows.Count == 1)
                {
                    this.Order = table.Rows[0].GetIntValue("Order");
                    this.Param = table.Rows[0].GetBooleanValue("Param");
                    this.Url = table.Rows[0].GetStringValue("Url");
                    this._webSite = table.Rows[0].GetIntValue("Website");
                    this.RequestType = new ReqType(table.Rows[0].GetIntValue("Req_Type"));
                    this.ResponseType = new RespType(table.Rows[0].GetIntValue("Resp_Type"));
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.LoadRequest", ex);
            }
        }

        public void Update()
        {
            try
            {
                Logger.Log.TraceAction("Request.UpdateRequest", "");
                var req = SqlWRequest.Update;
                var data = new DbConnect.DataAccess();
                data.AddParam(ParamWRequest.ID, this._ID);
                data.AddParam(ParamWRequest.Order, this.Order);
                data.AddParam(ParamWRequest.Url, this.Url);
                 data.AddParam(ParamWRequest.Param, this.Param);
                 data.AddParam(ParamWRequest.WebSite, this._webSite);
                 data.AddParam(ParamWRequest.ReqType, this.RequestType.ID);
                 data.AddParam(ParamWRequest.RespType, this.ResponseType.ID);
                data.Delete(req);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.UpdateRequest", ex);
            }

        }

        public int Create()
        {
            int Ok = 0;
            try
            {

                Logger.Log.TraceAction("Request.CreateRequest", "");
                var req = SqlWRequest.InsertInto;

                var data = new DbConnect.DataAccess();
               
                 data.AddParam(ParamWRequest.Order, this.Order);
                 data.AddParam(ParamWRequest.Url, this.Url);
                 data.AddParam(ParamWRequest.Param, this.Param);
                 data.AddParam(ParamWRequest.WebSite, this._webSite);
                 data.AddParam(ParamWRequest.ReqType, this.RequestType.ID);
                 data.AddParam(ParamWRequest.RespType, this.ResponseType.ID);
                Ok = data.Insert(req);

                this._ID = data.GetLastIDInserted(SqlWRequest.TableName);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.CreateRequest", ex);
                Ok = 0;
            }

            return Ok;
        }

        public void Delete()
        {
            try
            {
                Logger.Log.TraceAction("Request.DeleteRequest", "");

                foreach (var parameter in _parameters)
                {
                    DeleteParam(parameter.Name);
                }

                var req = SqlWRequest.Delete;
                var data = new DbConnect.DataAccess();
               
                 data.AddParam(ParamWRequest.ID, this._ID);
                data.Delete(req);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.DeleteRequest", ex);
            }
            finally
            {
       
            }
        }

        public void LoadParameter()
        {
            try
            {
                Logger.Log.TraceAction("Request.LoadParameter", "");

                var req = SqlParameter.SelectByRequest;
               
                var data = new DbConnect.DataAccess();
                 data.AddParam(ParamParameter.Req, this._ID);

               var table =  data.SelectWithParam(req);

               foreach (var row in table.Rows)
               {
                   var p = new Parameter(Int16.Parse(row.ToString()));
                   this.AddParam(p.Name, p.Value);
               }

            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.LoadParameter", ex);
            }
        }
        #endregion

        #endregion

        #region "Private functions"
        #endregion

        #region ReqType&RespType
        //used to load data from lib table
        // May run async after LoadWebsite Function
        //private void LoadLibelle()
        //{
        //    try
        //    {
        //        Logger.Log.TraceAction("Request.Loadlibelle", "");
        //        var req = SqlRequest.Select + SqlReqType.Lib +
        //             SqlRequest.From + SqlReqType.TableName +
        //             SqlRequest.Where + SqlReqType.ID + SqlRequest.Equal + this._reqType.ToString();

        //        var table = new DbConnect.DataAccess().Select(req);

        //        if (table.Rows.Count > 0)
        //        {
        //            this.RequestType = table.Rows[0]["ReqTypeLib"].ToString();
        //        }

        //         req = SqlRequest.Select + SqlRespType.RespTypeLib +
        //            SqlRequest.From + SqlRespType.TableName +
        //            SqlRequest.Where + SqlRespType.ID + SqlRequest.Equal + this._respType.ToString();

        //         table = new DbConnect.DataAccess().Select(req);

        //        if (table.Rows.Count > 0)
        //        {
        //            this.ResponseType = table.Rows[0]["RespTypeLib"].ToString();
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Logger.Log.TraceError("Request.LoadLibelle", ex);
        //    }


        //}



        private void InsertReqType(String Name)
        {

            try
            {
                Logger.Log.TraceAction("Request.InsertReqType", Name);
                if (Name.Length > 50)
                    return;
                var req = SqlReqType.InsertInto;
               
                var data = new DataAccess();
                 data.AddParam(ParamReqType.Lib, Name);
                data.Delete(req);;

            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.InsertReqType", ex);

            }
            finally
            {
            }
        }

        private void DeleteReqType(int IDReqType)
        {


            try
            {
                Logger.Log.TraceAction("Request.DeleteReqType", IDReqType.ToString());
                var req = SqlReqType.Delete;
               
                var data = new DataAccess();
                 data.AddParam(ParamReqType.ID, IDReqType);

                data.Delete(req);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.DeleteReqType", ex);

            }
            finally
            {
            }

        }

        private void UpdateReqType(int IDReqType, String Name)
        {

            try
            {
                Logger.Log.TraceAction("Request.UpdateReqType", Name);
                var req = SqlReqType.Update;
               
                var data = new DataAccess();
                 data.AddParam(ParamReqType.ID, IDReqType);
                 data.AddParam(ParamReqType.Lib, Name);

                data.Delete(req);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.UpdateReqType", ex);

            }
            finally
            {
            }
        }

        private int GetReqType(String Name)
        {
            var IDReqType = -1;
            try
            {
                Logger.Log.TraceAction("Request.GetReqType", Name);
                var req = SqlRequest.Select + SqlReqType.ID + SqlRequest.From + SqlReqType.TableName + SqlRequest.Where +
                     SqlReqType.Lib + SqlRequest.Equal + SqlRequest.AddQuote(Name);

               
                var data = new DataAccess();
                 data.AddParam(ParamReqType.Lib, Name);

                var table = data.SelectWithParam(req);
                if (table.Rows.Count > 0)
                {
                    IDReqType = table.Rows[0].GetIntValue("ID");
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.GetReqType", ex);

            }
            finally
            {
            }
            return IDReqType;

        }

        private String GetReqType(int IDReqType)
        {
            var LibReqType = "";
            try
            {
                Logger.Log.TraceAction("Request.GetReqType", IDReqType.ToString());
                var req = SqlRequest.Select + SqlReqType.Lib + SqlRequest.From + SqlReqType.TableName + SqlRequest.Where +
                     SqlReqType.ID + SqlRequest.Equal + IDReqType;

               
                var data = new DataAccess();
                 data.AddParam(ParamReqType.ID, IDReqType);

                var table = data.SelectWithParam(req);
                if (table.Rows.Count > 0)
                {
                    LibReqType = table.Rows[0].GetStringValue("LibReqType");
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.GetReqType", ex);

            }
            finally
            {
            }
            return LibReqType;

        }



        private void InsertRespType(String Name)
        {

            try
            {
                Logger.Log.TraceAction("Request.InsertRespType", Name);
                if (Name.Length > 50)
                    return;
                var req = SqlRespType.InsertInto;
               
                var data = new DataAccess();
                 data.AddParam(ParamRespType.Lib, Name);
                data.Delete(req);;

            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.InsertRespType", ex);

            }
            finally
            {
            }
        }

        private void DeleteRespType(int IDRespType)
        {


            try
            {
                Logger.Log.TraceAction("Request.DeleteRespType", IDRespType.ToString());
                var req = SqlRespType.Delete;
               
                var data = new DataAccess();
                 data.AddParam(ParamRespType.ID, IDRespType);

                data.Delete(req);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.DeleteRespType", ex);

            }
            finally
            {
            }

        }

        private void UpdateRespType(int IDRespType, String Name)
        {

            try
            {
                Logger.Log.TraceAction("Request.UpdateRespType", Name);
                var req = SqlRespType.Update;
               
                var data = new DataAccess();
                 data.AddParam(ParamRespType.ID, IDRespType);
                 data.AddParam(ParamRespType.Lib, Name);

                data.Delete(req);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.UpdateRespType", ex);

            }
            finally
            {
            }
        }

        private int GetRespType(String Name)
        {
            var IDReqType = -1;
            try
            {
                Logger.Log.TraceAction("Request.GetRespType", Name);
                var req = SqlRequest.Select + SqlRespType.ID + SqlRequest.From + SqlRespType.TableName + SqlRequest.Where +
                     SqlRespType.RespTypeLib + SqlRequest.Equal + SqlRequest.AddQuote(Name);

               
                var data = new DataAccess();
                 data.AddParam(ParamRespType.Lib, Name);

                var table = data.SelectWithParam(req);
                if (table.Rows.Count > 0)
                {
                    IDReqType = table.Rows[0].GetIntValue("ID");
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.GetRespType", ex);

            }
            finally
            {
            }
            return IDReqType;

        }

        private String GetRespType(int IDRespType)
        {
            var LibRespType = "";
            try
            {
                Logger.Log.TraceAction("Request.GetRespType", IDRespType.ToString());
                var req = SqlRequest.Select + SqlRespType.RespTypeLib + SqlRequest.From + SqlRespType.TableName + SqlRequest.Where +
                     SqlRespType.ID + SqlRequest.Equal + IDRespType;

               
                var data = new DataAccess();
                 data.AddParam(ParamReqType.ID, IDRespType);

                var table = data.SelectWithParam(req);
                if (table.Rows.Count > 0)
                {
                    LibRespType = table.Rows[0].GetStringValue("LibRespType");
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Request.GetRespType", ex);

            }
            finally
            {
            }
            return LibRespType;

        }

        #endregion


    }
}
